<template>
  <div>
    <el-container style="height: 100vh">
      <!-- 侧边栏 -->
      <el-aside width="200px">
        <!-- 
             default-active：默认激活
              background-color:背景色
              text-color：字体颜色
               active-text-color：激活字体颜色
               unique-opened	是否只保持一个子菜单的展开
               router	是否使用 vue-router 的模式，启用该模式会在激活导航时以 index 作为 path 进行路由跳转
         -->
        <el-menu
          :default-active="$route.path"
          class="el-menu-vertical-demo"
          background-color="rgba(0,0,0,0)"
          text-color="#fff"
          active-text-color="#ffd04b"
          unique-opened
          router
        >
          <el-menu-item index="/">
            <i class="el-icon-menu"></i>
            <span slot="title">首页</span>
          </el-menu-item>
          <!-- 动态侧边栏 -->
          <div v-for="item in userInfo.menus" :key="item.id">
            <!-- 目录 -->
            <el-submenu :index="item.id + ''" v-if="item.children">
              <template slot="title">
                <i :class="item.icon"></i>
                <span>{{ item.title }}</span>
              </template>
              <el-menu-item-group>
                <el-menu-item
                  :index="i.url"
                  v-for="i in item.children"
                  :key="i.id"
                  >{{ i.title }}</el-menu-item
                >
              </el-menu-item-group>
            </el-submenu>
            <!-- 菜单 -->
            <el-menu-item :index="item.url" v-else>
              <i class="el-icon-s-order"></i>
              <span slot="title">{{ item.title }}</span>
            </el-menu-item>
          </div>
        </el-menu>
      </el-aside>
      <el-container>
        <el-header>
          <span class="h4">{{ userInfo.username }}</span>
          <el-button class="exit" type="primary" plain @click="logout">退出登录</el-button>
        </el-header>
        <el-main>
          <el-breadcrumb
            v-if="$route.meta.title"
            separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>{{ $route.meta.title }}</el-breadcrumb-item>
          </el-breadcrumb>
          <!-- 二级路由视图 -->
          <router-view class="second-con"></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
export default {
  data() {
    return {};
  },
  computed: {
    ...mapGetters({
      userInfo: "userInfo", //用户信息
    }),
  },
  mounted() {},
  methods: {
    ...mapActions({
      changeUserInfo: "changeUserInfo",
    }),
    //退出登录
    logout() {
      this.changeUserInfo({});
      this.$router.replace("/login");
    },
  },
};
</script>
<style scoped lang="less">
@import "../../less/index.less";
.el-aside {
  background: @aside-bg;
}
.el-header {
  background: @header-bg;
}
.second-con {
  padding-top: @padding20;
}
.exit{
  float: right;
  margin-top: 10px;
}
.h4{
  line-height: 50px;
}
</style>